/*
	File:		CharacterSelectionState.cpp
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	CharacterSelectionState allows the player to choose a ship
*/

#include "CharacterSelectionState.h"

#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"

#include "Game.h"
#include "GameplayState.h"
#include "BitmapFont.h"

#include <cassert>


struct SMenuItem
{
public:
	int posX,posY;
	float scale;
	TCHAR* text;
	SMenuItem(TCHAR* _text,int _posX,int _posY,float _scale)
	{
		text = _text;
		posX=_posX;
		posY=_posY;
		scale = _scale;
	}
};

struct SBackgroundItem
{
public:
	int posX,posY,startY,startX;
	float scale;
	float sinWave;
	SBackgroundItem(float _posX,float _posY,float _scale)
	{
		startX = (int)_posX;
		startY = (int)_posY;
		posX=(int)_posX;
		posY=(int)_posY;
		scale = _scale;
		sinWave = 0;
	}
	void update( float fElapsedTime )
	{
		sinWave+=3.1415f* fElapsedTime ;
		posX+=(int)(100* fElapsedTime );//move left;
		posY = startY + (int)(sin(sinWave)*50);
		if(posX>CGame::GetInstance()->GetWidth())
			posX=-64;
	}
};


////////////////////////////////////////////////////////////
// SINGLETON!
/*static*/ CCharacterSelection* CCharacterSelection::GetInstance( void )
{
	// Static local object
	static CCharacterSelection s_Instance;

	return &s_Instance;
}


////////////////////////////////////////////////////////////
// CONSTRUCTOR
CCharacterSelection::CCharacterSelection(void)
{
	m_pDI		= nullptr;
	m_pTM		= nullptr;
	m_pFont		= nullptr;

	int space = 50;
	int start = 200;
	int current = start;

	menuVector.push_back(new SMenuItem(_T("Start Race"),100, current+=space, 1.0f));
	menuVector.push_back(new SMenuItem(_T("Ship Selection"),100, current+=space, 1.0f));
	menuVector.push_back(new SMenuItem(_T("Back"),100, current+=space, 1.0f));

	characterVector.push_back(new SCharacterItem(0.9f,345.0f,1100.0f,3.0f));
	characterVector.push_back(new SCharacterItem(0.6f,330.0f,1000.0f,4.0f));
	characterVector.push_back(new SCharacterItem(1.0f,350.0f,900.0f,3.5f));
	characterVector.push_back(new SCharacterItem(1.0f,350.0f,1050.0f,4.5f));
	characterVector.push_back(new SCharacterItem(1.0f,345.0f,950.0f,4.5f));

	m_nCursorImageID	= -1;
	for(int i = 0; i < 5; ++i)
		m_nShipImageID[i] = -1;

	m_nCursorPos		= 0;
}


////////////////////////////////////////////////////////////
// DESTRUCTOR
CCharacterSelection::~CCharacterSelection(void)
{
	for(unsigned int i = 0; i < menuVector.size(); ++i)
		delete menuVector[i];
	menuVector.clear();

	for(unsigned int i = 0; i < characterVector.size(); ++i)
		delete characterVector[i];
	characterVector.clear();
}


void CCharacterSelection::Enter( void )
{
	// Store wrappers
	m_pDI		= CSGD_DirectInput::GetInstance();
	m_pTM		= CSGD_TextureManager::GetInstance();
	m_pFont		= CGame::GetInstance()->GetFont();

	// Load assets
	m_nCursorImageID	= m_pTM->LoadTexture( 
		_T("resource/graphics/PN_MenuCursor.png") );
	m_nShipImageID[0]		= m_pTM->LoadTexture( 
		_T("resource/graphics/PN_ship1.png") );
	m_nShipImageID[1]		= m_pTM->LoadTexture( 
		_T("resource/graphics/PN_ship2.png") );
	m_nShipImageID[2]		= m_pTM->LoadTexture( 
		_T("resource/graphics/PN_ship3.png") );
	m_nShipImageID[3]		= m_pTM->LoadTexture( 
		_T("resource/graphics/PN_ship4.png") );
	m_nShipImageID[4]		= m_pTM->LoadTexture( 
		_T("resource/graphics/PN_ship5.png") );
	
	for(int i = 0; i < 5; ++i)
		characterVector[i]->imageID = i;

	shipBI = new SBackgroundItem(-64,100,64/512.0f);

	// Set other members
	m_nCursorPos		= 0;
	m_nMenuSize			= menuVector.size();

	m_nCharacter_CursorPos = 0;
	m_nCharacter_MenuSize = characterVector.size();
}

void CCharacterSelection::Exit( void )
{
	// Unload the assets
	if( m_nCursorImageID != -1 )
	{
		m_pTM->UnloadTexture( m_nCursorImageID );
		m_nCursorImageID = -1;
	}	// Unload the assets
	for(int i = 0; i < 5; ++i)
	{
		m_pTM->UnloadTexture( m_nShipImageID[i] );
		m_nShipImageID[i] = -1;
	}
	delete shipBI;

	// Reset the wrappers to null
	m_pDI		= nullptr;
	m_pTM		= nullptr;
	m_pFont		= nullptr;
}

////////////////////////////////////////////////////////////
// Input			- handle user input
/*virtual*/ bool CCharacterSelection::Input( void )
{
	if( m_pDI->KeyPressed( DIK_UP ) )
	{
		--m_nCursorPos;
		if(m_nCursorPos<0)
			m_nCursorPos = m_nMenuSize - 1;
	}
	else if( m_pDI->KeyPressed( DIK_DOWN ) )
	{
		++m_nCursorPos;
		if(m_nCursorPos == m_nMenuSize)
			m_nCursorPos = 0;
	}
	if( m_pDI->KeyPressed( DIK_ESCAPE ) )
	{
		if(m_nCursorPos == m_nMenuSize - 1)//Pressed escaped when it was already on escape
			return false;
		m_nCursorPos = m_nMenuSize -1;
	}

	if( m_pDI->KeyPressed( DIK_RETURN ) )
	{
		switch( m_nCursorPos )
		{
		case 0:		// Play Game
			{
				CGame::GetInstance()->PushState( CGameplayState::GetInstance() );
				return true;
			}
			break;
		case 2:		// Back
			{
				CGame::GetInstance()->PopState();
				return true;
			}
			break;
		}
	}


	if(m_nCursorPos == 1)//Character selection
	{
		if( m_pDI->KeyPressed( DIK_LEFT ) )
		{
			--m_nCharacter_CursorPos;
			if(m_nCharacter_CursorPos<0)
				m_nCharacter_CursorPos = m_nCharacter_MenuSize - 1;
		}
		else if( m_pDI->KeyPressed( DIK_RIGHT ) )
		{
			++m_nCharacter_CursorPos;
			if(m_nCharacter_CursorPos == m_nCharacter_MenuSize)
				m_nCharacter_CursorPos = 0;
		}
	}

	return true;
}				

////////////////////////////////////////////////////////////
// Update			- update game entities
/*virtual*/ void CCharacterSelection::Update( float fElapsedTime )
{
	sinWave+=3.1415f*fElapsedTime;
	shipBI->update(fElapsedTime);
}

////////////////////////////////////////////////////////////
// Render			- render game entities
/*virtual*/ void CCharacterSelection::Render( void )
{
	m_pTM->Draw(m_nShipImageID[0],shipBI->posX,shipBI->posY,shipBI->scale,shipBI->scale,nullptr,256,256,3.1415f/2);
	// Render the menu
	m_pFont->Draw( _T("Choose your character"), 100, 100, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );

	for(unsigned int i = 0; i < menuVector.size(); ++i)
	{
		D3DCOLOR color = D3DCOLOR_ARGB( 255, 255, 255, 255 );
		float scale = menuVector[i]->scale;
		if(m_nCursorPos==i)
		{
			color = D3DCOLOR_ARGB( (int)(sin(sinWave)*200)+55, 255, 255, 255 );
			scale += (sin(sinWave)*0.1f);
		}
		if((i == 1)&&m_nCursorPos==1)//character Selection
			m_pFont->Draw(_T("Press 'Left' or 'Right'"),menuVector[i]->posX,menuVector[i]->posY,scale, color);
		else
			m_pFont->Draw(menuVector[i]->text,menuVector[i]->posX,menuVector[i]->posY,scale, color);
	}	
	int nCursorX = menuVector[m_nCursorPos]->posX - 24;
	int nCursorY = menuVector[m_nCursorPos]->posY + 8;
	m_pTM->Draw( m_nCursorImageID, nCursorX, nCursorY);

	{
		//draw current ship spec.
		int startX = 700;
		int startY = 120;
		int offset = 30;
		TOSTRINGSTREAM output;

		output << _T("Stats: ");
		m_pFont->Draw( output.str().c_str(), startX, startY+=offset, 0.5f, D3DCOLOR_ARGB(255, 255, 255, 255) );
		output.str(_T(""));

		output << _T("Fire Rate: ") << characterVector[m_nCharacter_CursorPos]->FireRate;
		m_pFont->Draw( output.str().c_str(), startX, startY+=offset, 0.5f, D3DCOLOR_ARGB(255, 255, 255, 255) );
		output.str(_T(""));
		
		output << _T("Acceleration Rate: ") << characterVector[m_nCharacter_CursorPos]->AccelerationRate;
		m_pFont->Draw( output.str().c_str(), startX, startY+=offset, 0.5f, D3DCOLOR_ARGB(255, 255, 255, 255) );
		output.str(_T(""));
		
		output << _T("Max Speed: ") << characterVector[m_nCharacter_CursorPos]->MaxSpeed;
		m_pFont->Draw( output.str().c_str(), startX, startY+=offset, 0.5f, D3DCOLOR_ARGB(255, 255, 255, 255) );
		output.str(_T(""));
		
		output << _T("TurningSpeed: ") << characterVector[m_nCharacter_CursorPos]->TurningSpeed;
		m_pFont->Draw( output.str().c_str(), startX, startY+=offset, 0.5f, D3DCOLOR_ARGB(255, 255, 255, 255) );
		output.str(_T(""));

		m_pTM->Draw(m_nShipImageID[characterVector[m_nCharacter_CursorPos]->imageID],startX,startY,0.7f,0.7f,nullptr);
	}
}
